package org.hcf.utils.exceltemp;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.hcf.utils.excel.poi.base.exp.SimpleExcelHelper;
import org.hcf.utils.excel.poi.base.exp.header.TableHeader;
import org.hcf.utils.excel.poi.base.exp.ValidationDataHandler;

import java.util.*;


/**
 * @author changfu.he
 * @date 2021/9/4
 */
@Getter@Setter
public class EmpXlsxHandler extends SimpleExcelHelper<Map<String,Object>> {

    class NameHeader extends TableHeader<Map<String,Object>>{
        public NameHeader(String fieldName, int width){
            super(fieldName, width);
        }
        @Override
        public void dataCellout(Cell cell, Map<String,Object> map) {
            String name = null;
            if(Objects.isNull(name = (String) map.get("name"))){
                name = "";
            }
            cell.setCellValue(name);
        }
    }

    class DepHeader extends TableHeader<Map<String,Object>>{
        public DepHeader (String fieldName, int width){
            super(fieldName, width);
        }
        @Override
        public void dataCellout(Cell cell, Map<String, Object> map) {
            String dep = null;
            if(Objects.isNull(dep = (String) map.get("dep"))){
                dep = "";
            }
            cell.setCellValue(dep);
        }
    }

    class RoleHeader extends TableHeader<Map<String,Object>>{
        public RoleHeader (String fieldName, int width){
            super(fieldName, width);
        }
        @Override
        public void dataCellout(Cell cell, Map<String,Object> map) {
            String role = null;
            if(Objects.isNull(role = (String) map.get("role"))){
                role = "";
            }
            cell.setCellValue(role);
        }
    }

    class PhoneHeader extends TableHeader<Map<String,Object>>{
        public PhoneHeader (String fieldName, int width){
            super(fieldName, width);
        }
        @Override
        public void dataCellout(Cell cell, Map<String,Object> map) {
            String phone = null;
            if(Objects.isNull(phone = (String) map.get("phone"))){
                phone = "";
            }
            cell.setCellValue(phone);
        }
    }

    class PwdHeader extends TableHeader<Map<String,Object>>{
        public PwdHeader (String fieldName, int width){
            super(fieldName, width);
        }
        @Override
        public void dataCellout(Cell cell, Map<String,Object> map) {
            String pwd = null;
            if(Objects.isNull(pwd = (String) map.get("pwd"))){
                pwd = "";
            }
            cell.setCellValue(pwd);
        }
    }

    class IdentityNoHeader extends TableHeader<Map<String,Object>>{
        public IdentityNoHeader (String fieldName, int width){
            super(fieldName, width);
        }
        @Override
        public void dataCellout(Cell cell, Map<String,Object> map) {
            String identityNo = null;
            if(Objects.isNull(identityNo = (String) map.get("identityNo"))){
                identityNo = "";
            }
            cell.setCellValue(identityNo);
        }
    }

    @Override
    public List<TableHeader<Map<String,Object>>> initHeaders() {
        List<TableHeader<Map<String,Object>>> headers = new ArrayList<>();

        headers.add(new NameHeader("姓名", 4000));
        headers.add(new DepHeader("部门", 4000));
        headers.add(new RoleHeader("角色", 4000));
        headers.add(new PhoneHeader("手机号码", 4000));
        headers.add(new PwdHeader("密码", 4000) );
        headers.add(new IdentityNoHeader("身份证号", 4000));

        return headers;
    }


    protected Collection<String> depValidationDatas;
    protected Collection<String> roleValidationDatas;

    /** 请先初始化 validationDatas再 调用 initValidationDataHandlers*/
    @Override
    public List<ValidationDataHandler> initValidationDataHandlers() {
        List<ValidationDataHandler> handlers = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(this.depValidationDatas)) {
            handlers.add(new ValidationDataHandler(this, "depNameList", true, this.depValidationDatas,1));
        }
        if(CollectionUtils.isNotEmpty(this.roleValidationDatas)) {
            handlers.add(new ValidationDataHandler(this, "roleNameList", true, this.roleValidationDatas, 2));
        }
        return handlers;
    }


}
